#!/bin/sh 

stty -isig
trap ":" INT QUIT TSTP

if [ -e /proc/cmdline -a -z "$staplesrc" ]; then
    eval `cat /proc/cmdline`
fi

slow() {
    while read line; do
        sleep 1
        echo $line
    done
}

nogo() {
    msg=$1
    domain=`hostname -d`
    host=`hostname`
    echo $msg 1>&2
    echo "EHLO ${host}.${domain}
MAIL FROM: <root>
RCPT TO: <root@${domain}>
DATA
From: root
To: root@${domain}
Subject: ${host}: staple failed

Staple failed on host $host
$msg
rebooting in 300 seconds...
.
QUIT
" | slow | telnet smtp smtp > /dev/null 2>&1
    echo "rebooting in 300 seconds..."
    sleep 300
    /sbin/reboot -f
}

stapledir=/boot/staple

echo "Trying to load staple from \"$staplesrc\"..."

if [ ! -e "$stapledir" ]; then
    mkdir $stapledir
    if [ ! -e "$stapledir" ]; then
        nogo "$stapledir doesn't exits, can't mount staple database"
    fi
fi

if [ "$staplesrc" = "" ]; then
    nogo "staplesrc variable isn't set, don't know from where to mount staple database"
fi

mount -n -o nolock,ro -t nfs $staplesrc $stapledir
code=$?

if [ "$code" != "0" ]; then
    nogo "Can't mount staple database from $staplesrc"
fi

PERL5LIB=$stapledir/lib $stapledir/bin/stapleboot-dev
code=$?

if [ "$code" != "0" ]; then
    echo "stapleboot failed, please notify the system administrator"
    nogo "stapleboot failed, exit status $code"
fi

stty isig
